package {
	import ypmits.setup.EasySetup;
	import ypmits.setup.EasySetupInput;
	import ypmits.setup.view.FullScreenButtonGraphic;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * @author Dave Lenz
	 */
	public class BitmapProximityTest extends Sprite {
		
		private var rows : int = 100;
		private var columns : int = 70;
		private var sprites : Array = [];
		private var s : Sprite;
		private var ddx : Number;
		private var ddy : Number;
		private var dist : Number;
		private var scale : Number;
		private var factor : Number = 1;
		private var max_dist : Number = 200;

		public function BitmapProximityTest()
		{
			var _i : EasySetupInput = new EasySetupInput( );
			_i.fullscreenbuttongraphic = new FullScreenButtonGraphic( 15, 15, 0xFFFFFF, stage );
			
			EasySetup.instance.init( this, _i );
			EasySetup.instance.debug = true;
			
			for (var i : int = 0; i < columns; i++) {
				for (var j : int = 0; j < rows; j++) {
					var sprite : Sprite = new Sprite();
					sprite.mouseChildren = sprite.mouseEnabled = false;
					sprite.x = i * 20;
					sprite.y = j * 20;
					with( sprite ) {
						graphics.beginFill( Math.random() * 0xFF9900 );
						graphics.drawRect(-20, -20, 40, 40 );
						graphics.endFill();
					}
					addChild( sprite );
					
					sprites.push( sprite );
				}
			}
			
			addEventListener( Event.ENTER_FRAME, enterHandler );
			
			EasySetup.instance.stage.addEventListener( MouseEvent.MOUSE_DOWN, clickHandler );
			EasySetup.instance.stage.addEventListener( MouseEvent.MOUSE_UP, clickHandler );
		}

		
		private function enterHandler( e : Event ) : void 
		{
			for ( var i : int = 0; i < sprites.length; i++ ) {
				s = Sprite( sprites[i] );
				ddx = mouseX - s.x;
				ddy = mouseY - s.y;
				dist = Math.sqrt( (ddx * ddx) + (ddy * ddy) );
				if( dist < max_dist ) {
					scale = Math.cos( dist/(max_dist * .5)) * factor;
					s.scaleX = s.scaleY = scale;
				} else {
					s.scaleX = s.scaleY = 1;
				}
			}
		}

		
		private function clickHandler( e : Event ) : void 
		{
			if( e.type == MouseEvent.MOUSE_DOWN ) {
				factor = .5;
			} else if( e.type == MouseEvent.MOUSE_UP ) {
				factor = 1;
			}
		}
	}
}
